package com.lockapp;

import com.lockapp.database.BlockDAO;
import com.lockapp.database.SenhaDAO;
import com.lockapp.entity.Block;
import com.lockapp.entity.Senha;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class DefinirSenhaActivity extends Activity{

	private EditText digitoUm;
	private EditText digitoDois;
	private EditText digitoTres;
	private EditText digitoQuatro;
	private TextView textView;
	
	private SenhaDAO dao;
	private BlockDAO daoBlock;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_senha);
		
		textView = (TextView) findViewById(R.id.textViewTexto);
		digitoUm = (EditText) findViewById(R.id.editText1);
		digitoDois = (EditText) findViewById(R.id.editText2);
		digitoTres = (EditText) findViewById(R.id.editText3);
		digitoQuatro = (EditText) findViewById(R.id.editText4);
		
		dao = new SenhaDAO(this);
		daoBlock = new BlockDAO(this);
		
		textView.setText("Defina uma senha nos campos abaixo para continuar");
		digitoUm.requestFocus();
		digitoUm.setOnKeyListener(new OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if(digitoUm.getText().toString().length() > 0){
					digitoDois.requestFocus();
				}
				return false;
			}
		});
		
		digitoDois.setOnKeyListener(new OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if(digitoDois.getText().toString().length() > 0){
					digitoTres.requestFocus();
				}
				return false;
			}
		});
		
		digitoTres.setOnKeyListener(new OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if(digitoTres.getText().toString().length() > 0){
					digitoQuatro.requestFocus();
				}
				return false;
			}
		});
		
		digitoQuatro.setOnKeyListener(new OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if(digitoQuatro.getText().toString().length() > 0){
					Senha senha = new Senha();
					senha.setDigitoUm(Integer.parseInt(digitoUm.getText().toString()));
					senha.setDigitoDois(Integer.parseInt(digitoDois.getText().toString()));
					senha.setDigitoTres(Integer.parseInt(digitoTres.getText().toString()));
					senha.setDigitoQuatro(Integer.parseInt(digitoQuatro.getText().toString()));

					dao.insert(senha);
					
					Block block = new Block();
					block.setLiberacao(0);
					block.setNomeApp("LockApp");
					block.setIconeApp(null);
					block.setArrayImg(null);
					//tem que ser o nome da classe
					block.setPacoteApp("com.lockapp/.PrincipalActivity");
					daoBlock.inserir(block);
					
					startActivity(new Intent(DefinirSenhaActivity.this, PrincipalActivity.class));
					
					//codigo inserir lockapp em base de dados bloqueado
					
					
					finish();
				}
				return false;
			}
		});
	}
	
	public void mensagem(String msg){
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}

}
